//-----------------Sonic CD Title Card Script-----------------//
//--------Scripted by Christian Whitehead 'The Taxman'--------//
//-------Unpacked By Rubberduckycooly's Script Unpacker-------//

// Aliases
private alias object.value0 : Object.BarPos
private alias object.value1 : Object.TextPos
private alias object.value2 : Object.TextSize
private alias object.value3 : Object.Timer
private alias object.value4 : Object.FadeR
private alias object.value5 : Object.FadeG
private alias object.value6 : Object.FadeB
private alias object.value7 : Object.CharPos

// SS Sonic aliases
// WARNING: Variable VAR_PLAYERCONTROLMODE does not exist in RSDKv4!
private alias object[0].controlMode : SSSonic.ControlMode

// HUD Alias
private alias object.value1 : HUD.TimeSeconds
private alias object.value2 : HUD.TimeFrames

// States
private alias 0 : TITLECARD_INTRO
private alias 1 : TITLECARD_FADEIN
private alias 2 : TITLECARD_SLIDEIN
private alias 3 : TITLECARD_DISPLAY
private alias 4 : TITLECARD_SLIDEOUT

// Languages
private alias 0 : LANG_ENGLISH
private alias 1 : LANG_FRENCH
private alias 2 : LANG_ITALIAN
private alias 3 : LANG_DEUTSCH
private alias 4 : LANG_SPANISH
private alias 5 : LANG_JAPANESE

// ControlMode Aliases
private alias -1 : CONTROLMODE_NONE
private alias 0 : CONTROLMODE_NORMAL

// Game Mode Aliases
private alias 1 : MODE_TIMEATTACK

// Priority
private alias 1 : PRIORITY_ACTIVE

private value Fade_Colour = 0;


event ObjectDraw
	// Set the starting time values
	// (Rather than disabling the HUD timer while the title card is active,
	//  its values are simply reset here every frame until no longer needed)
	HUD.TimeSeconds[4] = 100
	HUD.TimeFrames[4] = 0

	switch object.state
	case TITLECARD_INTRO
		object.timer ++
		stopMusic()
		stage.state = STAGE_PAUSED
		if object.timer > 550
			object.state ++
			object.timer = 384
			object[21].state = 1
		endif
		break
	case TITLECARD_FADEIN
		stage.state = STAGE_RUNNING
		if Object.Timer > 0
			if Object.Timer == 256
				// Start the stage music!
				PlayMusic(0)

				// Free Sonic's control
				// (But, it gets locked again right after anyways...)
				SSSonic.ControlMode = CONTROLMODE_NORMAL
			end if

			SetScreenFade(Object.FadeR, Object.FadeG, Object.FadeB, Object.Timer)
			Object.Timer -= 8
		else
			// Make the Title Card start appearing

			object.state = TITLECARD_SLIDEIN
			Object.Timer = 0
		end if

		// Lock Sonic's control
		SSSonic.ControlMode = CONTROLMODE_NONE
		break

	case TITLECARD_SLIDEIN
		temp0 = 144
		temp0 += Object.CharPos

		if Object.TextPos > temp0
			// Slide the text to the left
			Object.TextPos -= 8

			// Now, this part got changed in an update

		else
			// -> Updated version - both the text AND the bar have to be on screen for the title card to progress

			if Object.BarPos == 0
				object.state = TITLECARD_DISPLAY
			end if
		end if

		if Object.BarPos < 0
			// Move the Bar down
			Object.BarPos += 8

			// There used to be code here in older versions, but not anymore...
			// -> Initial version - only the bar needed to be in the proper position for the title card to continue
			//    This had text get incorrectly often

		end if
		break

	case TITLECARD_DISPLAY
		// Hold for a moment to let the Player see what a nice Title Card this is

		if Object.Timer == 160
			// Time's up, start sliding out!
			object.state = TITLECARD_SLIDEOUT
		else
			Object.Timer++
		end if
		break

	case TITLECARD_SLIDEOUT
		if Object.TextPos < 408

			// Move the text to the right, and...
			Object.TextPos += 16

			// ...move the bar upwards as well
			Object.BarPos -= 16

		else
			// The Title Card's job is done, despawn it
			ResetObjectEntity(20, TypeName[Blank Object], 0, 0, 0)

			// Free Sonic's control, for real this time
			SSSonic.ControlMode = CONTROLMODE_NORMAL

			if Options.GameMode == MODE_TIMEATTACK
				// If in time attack, start proper time progression
				stage.timeEnabled = true
			end if
		end if
		break

	end switch

	// Before drawing, make sure this object hasn't been blanked already
	// (Under certain circumstances, this can otherwise cause major graphical issues!!)
	if object.type > TypeName[Blank Object]
		temp0 = 136
		temp0 += Object.CharPos

		// Draw the Title Card Bar
		DrawSpriteScreenXY(0, temp0, Object.BarPos)

		// And then draw the Sonic CD bar and the weird three lines
		DrawSpriteScreenXY(5, Object.TextPos, 64)
		DrawSpriteScreenXY(6, Object.TextPos, 64)
		DrawSpriteScreenXY(7, Object.TextPos, 64)

#platform: Use_Decomp // Creates a black rectangle that fills the empty space on wider resolutions	
		temp0 = Object.TextPos
		temp0 += 150
		DrawRect(temp0, 147, screen.xsize, 9, 0, 0, 0, 255)
#endplatform

		// And then Draw the Round Name
		// (Letters start at Sprite Frame 8)
		temp0 = 8
		while temp0 < Object.TextSize
			DrawSpriteScreenXY(temp0, Object.TextPos, 64)
			temp0++
		loop
	end if

end event


event ObjectStartup

	LoadSpriteSheet("Special/ScoreScreen.gif")
	
	
	// Place the Title Card object into the level and setup its values
	object[20].type = TypeName[TitleCard]
	Object.Timer[20] = 384

	// Make the Title Card always active, and have it draw above everything
	object[20].priority = PRIORITY_ACTIVE_PAUSED
	object[20].drawOrder = 6

	// Set initial Bar & Text Positions
	Object.BarPos[20] = -216
	Object.TextPos[20] = 336


	Object.CharPos[20] = screen.xcenter
	Object.CharPos[20] -= 160

	if Object.CharPos[20] > 3
		Object.CharPos[20] -= 4
	end if

	Object.CharPos[20] &= 248

	Object.TextPos[20] += Object.CharPos[20]
	Object.TextPos[20] += Object.CharPos[20]

	// "SPECIAL STAGE" is 12 letters
	Object.TextSize[20] = 11

	// Bump the value by 8 to correspond with the order of the Sprite Frames
	Object.TextSize[20] += 8

	// Preserve the colour used from the last fade. Fade_Colour is stored as RRGGBB, in hex

	Object.FadeR[20] = Fade_Colour
	Object.FadeR[20] >>= 16

	Object.FadeG[20] = Fade_Colour
	Object.FadeG[20] &= 0x00FF00
	Object.FadeG[20] >>= 8

	Object.FadeB[20] = Fade_Colour
	Object.FadeB[20] &= 0x0000FF

	object[21].type = TypeName[Debug Text]
	
	// 0 - Red Bar Frame
	SpriteFrame(0, 0, 32, 184, 224, 37)

	// These next few frames are unused leftovers from the maingame Title Card script
	// They appear as garbage sprites here since they use values from the normal Global Display sheet,
	// and not the Special Stage version of the sheet

	// 1-3 - Act Bubbles, appear as random texts with this sheet
	SpriteFrame(96, 96, 48, 48, 41, 1)
	SpriteFrame(96, 96, 48, 48, 90, 1)
	SpriteFrame(96, 96, 48, 48, 139, 1)

	// 4 - "Zone" text, appears as part of BG with this sheet though
	SpriteFrame(64, 97, 48, 16, 41, 67)

	// All the letters used by the Title Card
	// They got updated a bit post-release, we're using the updated version here

	// 5 - Main part of the "SONIC THE HEGDEHOG CD" bar	

	// Workaround for steam datafile to show this sprite correctly
#platform: Use_Origins
	SpriteFrame(72, 81, 160, 16, 60, 227) // #5  - Sonic The Hedgehog CD
#endplatform

#platform: Use_Standalone
#platform: Standard
	SpriteFrame(64, 81, 160, 16, 52, 227) // #5  - Sonic The Hedgehog CD
#endplatform
#endplatform

#platform: Use_Standalone
#platform: Mobile
	SpriteFrame(72, 81, 160, 16, 60, 227) // #5  - Sonic The Hedgehog CD
#endplatform
#endplatform

	// 6 - Continuation of the Sonic CD bar
	SpriteFrame(16, 81, 160, 16, 52, 227)

	// 7 - Set of three lines
	SpriteFrame(100, 32, 24, 48, 182, 124)

	// 8-19 - Individual letters spelling out "Special Stage"
	SpriteFrame(0, 0, 16, 55, 207, 124) // #8  - S
	SpriteFrame(17, 32, 10, 23, 176, 180) // #9  - P
	SpriteFrame(28, 32, 9, 23, 187, 180) // #10 - E
	SpriteFrame(38, 32, 8, 23, 197, 180) // #11 - C
	SpriteFrame(47, 32, 6, 23, 206, 180) // #12 - I
	SpriteFrame(54, 32, 11, 23, 212, 180) // #13 - A
	SpriteFrame(66, 32, 9, 23, 176, 203) // #14 - L

	SpriteFrame(0, 56, 16, 55, 207, 124) // #15 - S
	SpriteFrame(17, 56, 10, 23, 186, 203) // #16 - T
	SpriteFrame(28, 56, 11, 23, 212, 180) // #17 - A
	SpriteFrame(40, 56, 10, 23, 197, 203) // #18 - G
	SpriteFrame(51, 56, 9, 23, 187, 180) // #19 - E

#platform: Use_Origins
	// There aren't any Special Stage missions nor are any of them part of the Boss Rush, so god knows why this code is here

	if game.playMode == BOOT_PLAYMODE_MISSION
		temp0 = true
	else
		if game.playMode == BOOT_PLAYMODE_BOSSRUSH
			temp0 = true
		else
			temp0 = false
		end if
	end if

	if temp0 != false
		PlayMusic(0)

		game.missionCondition = 0
		game.missionEnd = false

// WARNING: Variable VAR_PLAYERCONTROLMODE does not exist in RSDKv4!
		object[2].controlMode = CONTROLMODE_NORMAL
		stage.timeEnabled = true

		ResetObjectEntity(20, TypeName[Blank Object], 0, 0, 0)
		Warp.XPos = 0
	end if
#endplatform
end event


// ========================
// Editor Subs
// ========================

event RSDKDraw
	Object.TextSize = 12
	Object.TextSize += 8

	temp0 = 136
	temp0 += Object.CharPos
	temp0 <<= 16

	temp1 = 0x500000
	temp1 += object.ypos

	DrawSpriteXY(0, object.xpos, object.ypos)
	DrawSpriteXY(5, object.xpos, temp1)
	DrawSpriteXY(6, object.xpos, temp1)
	DrawSpriteXY(7, object.xpos, temp1)

	temp0 = 8
	while temp0 < Object.TextSize
		DrawSpriteXY(temp0, object.xpos, temp1)
		temp0++
	loop
end event


event RSDKLoad
	LoadSpriteSheet("Special/ScoreScreen.gif")
	// 0 - Red Bar Frame
	SpriteFrame(0, 0, 32, 184, 224, 37)

	// 1-3 - Act Bubbles, not used here.
	SpriteFrame(96, 96, 48, 48, 41, 1)
	SpriteFrame(96, 96, 48, 48, 90, 1)
	SpriteFrame(96, 96, 48, 48, 139, 1)

	// 4 - "Zone" text, not used here.
	SpriteFrame(64, 97, 48, 16, 41, 67)

	// 5 - Main part of the "SONIC THE HEGDEHOG CD" bar
	SpriteFrame(72, 81, 160, 16, 60, 227)

	// 6 - Continuation of the Sonic CD bar
	SpriteFrame(16, 81, 160, 16, 52, 227)

	// 7 - Set of three lines
	SpriteFrame(100, 32, 24, 48, 182, 124)

	// 8-19 - Individual letters spelling out "Special Stage"
	SpriteFrame(0, 0, 16, 55, 207, 124) // #8  - S
	SpriteFrame(17, 32, 10, 23, 176, 180) // #9  - P
	SpriteFrame(28, 32, 9, 23, 187, 180) // #10 - E
	SpriteFrame(38, 32, 8, 23, 197, 180) // #11 - C
	SpriteFrame(47, 32, 6, 23, 206, 180) // #12 - I
	SpriteFrame(54, 32, 11, 23, 212, 180) // #13 - A
	SpriteFrame(66, 32, 9, 23, 176, 203) // #14 - L

	SpriteFrame(0, 56, 16, 55, 207, 124) // #15 - S
	SpriteFrame(17, 56, 10, 23, 186, 203) // #16 - T
	SpriteFrame(28, 56, 11, 23, 212, 180) // #17 - A
	SpriteFrame(40, 56, 10, 23, 197, 203) // #18 - G
	SpriteFrame(51, 56, 9, 23, 187, 180) // #19 - E


end event
